package com.doyens.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.reggie.commons.R;
import com.doyens.reggie.domain.Employee;
import com.doyens.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * 修改产生问题的原因
 *  用户的id是 18位的数值类型
 *  然而js最多 能16位数值类型
 *
 *  解决：id使用字符串表示   将对象转成json的时候 long--->string
 */
@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    // 根据id查询
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

    // 修改状态
    @PutMapping //通用修改
    public R<String> updateEmployee(@RequestBody Employee employee,HttpServletRequest request){//{id=1 status=0 username=null name=null tel=null.............}
        HttpSession session = request.getSession();
        Employee emp = (Employee) session.getAttribute("emp");

        // 补全更新用户和更新时间
      /*  employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(emp.getId());*/

        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    // 实现员工的分页查询功能
    @GetMapping("page")
    public R<Page> page(int page,int pageSize,@RequestParam(value = "name",required = false,defaultValue = "") String name){
        // 封装分页参数
        Page<Employee> params = new Page<>(page, pageSize);
        // 封装条件
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(name),Employee::getName,name);
        Page<Employee> pb = employeeService.page(params, wrapper);
        return R.success(pb);// code:1  msg:null  data:pb
    }

    // 添加员工
    @PostMapping
    public R<String> saveEmployee(@RequestBody Employee employee,HttpServletRequest request){
        HttpSession session = request.getSession();
        Employee emp = (Employee) session.getAttribute("emp");
        // 补全相关的信息
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setStatus(1);
      /*  employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(emp.getId());
        employee.setUpdateUser(emp.getId());*/
        employeeService.save(employee);
        return R.success("添加成功");
    }

    // 定义退出的功能
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("emp");
        return R.success("退出");//code=1  msg=null  data="退出"
    }

    // 定义登录的方法
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){// 123456
        // 登录的逻辑
        //1.根据用户名查询数据库 employee.getUsername
        String username = employee.getUsername();
        Employee emp = employeeService.getOne(new LambdaQueryWrapper<Employee>().eq(Employee::getUsername, username));
        //2.查询不到就是用户名不存在
        if(emp == null){
            return R.error("用户名不存在");
        }
        //3.将密码进行md加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //4.和查询的做比较
        if(!password.equals(emp.getPassword())){
            return R.error("密码错误");
        }
        //5.判断用户的状态 status == 0 说明被禁用
        if(emp.getStatus() == 0){
            return R.error("账号已禁用");
        }
        //6.登录成功
        // 登录成功 将当前登录用户信息保存到session
        HttpSession session = request.getSession();
        session.setAttribute("emp",emp);
        return R.success(emp); //code=1 msg=null data:emp
    }

}
